/**
 * Copyright  2025, Hai Yue Xing He Technology Co., Ltd
 * 
 * @author        ZHAO LIMIN
 * @version       1.0.1
 * @since         2025-04
 */
import { HGeometry } from "./HYXHGeometry";
import { ICartesian } from "./Cartesian"; 
import { UNION_NUMBER_ARRAY } from "./HDataType";
import { Check } from './Check'
import { defined } from './Defined'
import { Cartesian3 } from "./Cartesian3";
/**
 * 直线
 */
export class HLine implements HGeometry {
    /* 直线开始坐标 */
    public start: ICartesian;
    /* 直线结束坐标 */
    public end: ICartesian;
    public center:ICartesian;
    /* 索引 */
    public id: number = 0;              

    constructor(
        start_x:number,
        start_y:number, 
        start_z:number,
        end_x:number, 
        end_y:number,
        end_z:number)
    {
        this.start = {x: start_x, y: start_y, z: start_z};
        this.end = {x: end_x, y: end_y, z: end_z};
        this.center = {
            x: (start_x + end_x) / 2,
            y: (start_y + end_y) / 2,
            z: (start_z + end_z) / 2, 
        }
    }
    pack(layer: number, array: UNION_NUMBER_ARRAY, startIndex: number): UNION_NUMBER_ARRAY {
        Check.defined('array', array)
        if (!defined(array)) {
            array = new Array<number>();
        }    
        if (startIndex < 0) {
            startIndex = 0;
        }
        Cartesian3.pack(this.start, array, startIndex);
        startIndex += this.pckagedLength();
        Cartesian3.pack(this.start, array, startIndex);
        return array;
    }
    public unpack(layer: number, array: UNION_NUMBER_ARRAY, startIndex: number = 0) {
        Check.defined('array', array);
        if (startIndex < 0) {
            startIndex = 0;
        }
        Cartesian3.unpack(array, startIndex, this.start);
        startIndex += this.pckagedLength();
        Cartesian3.unpack(array, startIndex, this.end);
    }
    public update(): boolean {
        this.center = {
            x: (this.start.x + this.end.x ) / 2,
            y: (this.start.y + this.end.y ) / 2,
            z: (this.start.z + this.end.z) / 2, 
        }
        return true;
    } 
    public pckagedLength(): number {
       return Cartesian3.packedLength;
    }

    public pushBack(layer: number, newPoint: ICartesian): number {
        throw new Error("Method not implemented.");
    }
    public equals(other: HGeometry): boolean {
        if (other instanceof HLine) {
            return (Cartesian3.equals(this.start, other.start) &&
                     Cartesian3.equals(this.end, other.end) ) ||
                   (Cartesian3.equals(this.start, other.end) &&
                     Cartesian3.equals(this.end, other.start));
        }
        return false;
    }  

}